﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.MainModule.Entities;
using Presentation.Web.MVC.Client.ViewModels;
using System.Web.Routing;
using Application.MainModule.CustomersMTSManagement;


namespace Presentation.Web.MVC.Client.Controllers
{
    /// <summary>
    /// This controller handles all the actions related to a customer.
    /// </summary>
    [HandleError]
    public class CustomerMTSController : Controller
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomerMTSController"/> class.
        /// </summary>
        /// <param name="customerService">The customer service.</param>
        public CustomerMTSController(ICustomerMTSManagementService customerService)
        {
            _customerService = customerService;
        }

        #endregion

        #region Members

        /// <summary>
        /// Instance of the customer service.
        /// </summary>
        private ICustomerMTSManagementService _customerService;

        /// <summary>
        /// Display a paged listing of the existing customers.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">The page size.</param>
        /// <returns></returns>
        public ActionResult Index(int page, int pageSize)
        {

            IList<CustomerMTS> customers = _customerService.FindPagedCustomerMTS(page, pageSize);
            return View(new CustomerMTSListViewModel(customers, page, pageSize));
        }

        /// <summary>
        /// Shows a specific customer in detail.
        /// </summary>
        /// <param name="customerId">The customer code.</param>
        /// <returns>A view of the customer details.</returns>
        public ActionResult Details(Guid customerId)
        {
            //Get the customer aggregate.
            CustomerMTS customer = _customerService.FindCustomerMTSById(customerId);
            //Display its details.
            return View(customer);
        }


        /// <summary>
        /// Displays a form to create a new CustomerMTS.
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// Creates the specified customer based on the request data and displays it.
        /// </summary>
        /// <param name="customer">The customer to add.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(CustomerMTS customer)
        {
            //Check if there aren't validation errors on the customer.
            if (ModelState.IsValid)
            {
                //Add the customer to the data base.
                _customerService.AddCustomerMTS(customer);
                //Show the fist page of the customers list.
                return RedirectToAction("Index", new { page = 0, pageSize = 10 });
            }
            else
            {
                //Return the Create view and display the validation errors.
                return View();
            }
        }


        /// <summary>
        /// Displays a form to edit the customer.
        /// </summary>
        /// <param name="customerId">The customer code.</param>
        /// <returns></returns>
        public ActionResult Edit(Guid customerId)
        {
            //Get the customer aggregate to edit.
            CustomerMTS customer = _customerService.FindCustomerMTSById(customerId);
            //Return the
            return View(customer);
        }


        /// <summary>
        /// Saves the changes on the editions performed to the specified customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <returns>The details of the customer.</returns>
        [HttpPost]
        public ActionResult Edit(CustomerMTS customer)
        {
            if (ModelState.IsValid)
            {
                _customerService.ChangeCustomerMTS(customer);
                return RedirectToAction("Details", new RouteValueDictionary() { { "customerId", customer.UserId } });
            }
            else
            {
                return View();
            }
        }


        /// <summary>
        /// Deletes the specified customer code.
        /// </summary>
        /// <param name="customerId">The customer code.</param>
        /// <returns>The details of the customer.</returns>
        [HttpDelete]
        public ActionResult Delete(Guid customerId)
        {
            CustomerMTS customer = _customerService.FindCustomerMTSById(customerId);
            _customerService.RemoveCustomerMTS(customer);
            return RedirectToAction("Index", new RouteValueDictionary() { { "page", 0 }, { "pageSize", 10 } });
        }

        #endregion

    }
}